using System;
using System.Threading.Tasks;
using System.Windows.Input;
using CPlatePrintSys.Domain.Models;
using CPlatePrintSys.Domain.Services;
using Prism.Commands;
using Prism.Mvvm;

namespace CPlatePrintSys.Win.ViewModels
{
    public class LayerColorConfigViewModel : BindableBase
    {
        private readonly IGlobalConfigService _configService;

        // 图层颜色配置
        private LayerColorConfig _layerColorConfig;
        public LayerColorConfig LayerColorConfig
        {
            get { return _layerColorConfig; }
            set { SetProperty(ref _layerColorConfig, value); }
        }

        // 命令
        public ICommand SelectLineColorCommand { get; }
        public ICommand SelectTextColorCommand { get; }
        public ICommand SelectInfoColorCommand { get; }

        public LayerColorConfigViewModel(IGlobalConfigService configService)
        {
            _configService = configService;

            SelectLineColorCommand = new DelegateCommand(() => SelectColor("Line"));
            SelectTextColorCommand = new DelegateCommand(() => SelectColor("Text"));
            SelectInfoColorCommand = new DelegateCommand(() => SelectColor("Info"));

            // 初始化配置对象
            LayerColorConfig = new LayerColorConfig
            {
                LineLayerColor = "#FF0000",
                TextLayerColor = "#0000FF",
                InfoLayerColor = "#00FF00"
            };

            LoadConfig();
        }

        private async void LoadConfig()
        {
            try
            {
                LayerColorConfig = await _configService.GetLayerColorConfigAsync();
            }
            catch (Exception ex)
            {
                // 如果加载失败，使用默认颜色
                LayerColorConfig = new LayerColorConfig
                {
                    LineLayerColor = "#FF0000",
                    TextLayerColor = "#0000FF",
                    InfoLayerColor = "#00FF00"
                };
            }
        }

        public async Task SaveLayerColor()
        {
            try
            {
                await _configService.UpdateLayerColorConfigAsync(LayerColorConfig, "System");
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show($"保存图层颜色配置失败: {ex.Message}", "错误", 
                    System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }
        }

        // 添加颜色更新方法，确保触发UI更新
        public void UpdateLineColor(string color)
        {
            LayerColorConfig.LineLayerColor = color;
            RaisePropertyChanged(nameof(LayerColorConfig));
        }

        public void UpdateTextColor(string color)
        {
            LayerColorConfig.TextLayerColor = color;
            RaisePropertyChanged(nameof(LayerColorConfig));
        }

        public void UpdateInfoColor(string color)
        {
            LayerColorConfig.InfoLayerColor = color;
            RaisePropertyChanged(nameof(LayerColorConfig));
        }

        private async void SelectColor(string layerType)
        {
            try
            {
                var colorDialog = new System.Windows.Forms.ColorDialog();
                if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    var color = colorDialog.Color;
                    var hexColor = $"#{color.R:X2}{color.G:X2}{color.B:X2}";

                    switch (layerType)
                    {
                        case "Line":
                            UpdateLineColor(hexColor);
                            break;
                        case "Text":
                            UpdateTextColor(hexColor);
                            break;
                        case "Info":
                            UpdateInfoColor(hexColor);
                            break;
                    }

                    // 立即保存配置
                    await SaveLayerColor();
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show($"选择颜色失败: {ex.Message}", "错误", 
                    System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }
        }
    }
} 